let modelManager =require("ModelManager");

cc.Class({
    extends: cc.Component,

    properties: {

        canvasRoot:{
            default:null,
            type:cc.Node,

        },

        //一般预制体
        blocksPrefab:{
            default:[],
            type:[cc.Prefab],

        },
        //一般 表
        _CollisionList:{
            default:[],
            type:[cc.Node],
        },
        //道具
        PropPrefab:{
            default:[],
            type:[cc.Prefab],
        },
        //道具表



        refreshTime:15,
        difficultyLevel:1,
        difficultyAddTime:50,

        _timeAccumulate:0,

        _isStart:false,

        _layout:{
            default:null,
            type:cc.Node,
        },

        StartLayout:{
            default:[],
            type:[cc.Prefab],
        },

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this._CollisionList =new Array();
        //cc.log("root 宽度"+(this.node.width));
        //this.PlayGame();
    },

    StartGame(){

        this._isStart =true;
        let CollisionModule =require("CollisionModule");

        let randomvalue= cc.random0To1()*(this.StartLayout.length-1);
        //cc.error("随机值是"+randomvalue);
        this._layout= cc.instantiate(this.StartLayout[Math.round(randomvalue)]);
        cc.director.getScene().addChild(this._layout);
        for(let temp of this._layout.children){
            if(temp.name==="New Sprite(Splash)"){
                let rigi= temp.addComponent(cc.RigidBody);
                rigi.enabledContactListener =true;
                rigi.type =cc.RigidBodyType.Static;
                let box =temp.addComponent(cc.PhysicsBoxCollider);
               // cc.log(typeof temp);

                box.size.width =temp.width;
                box.size.height =temp.height;
                box.apply();

                temp.addComponent(CollisionModule);
            }

        }
        this.scheduleOnce(function () {

            if(this._isStart ===false){
                return;
            }

            this.SpawnGameObj();

            console.log("开始生成物体");
            this.schedule(this.LoopSpawn,this.refreshTime);
            //道具循环
            this.schedule(this.LoopPropSpawn,this.refreshTime);
            //控制移动循环
            this.schedule(this.LoopMove,this.refreshTime);
        },2);



    },



    //普通物品生成
    LoopSpawn(){
        this.SpawnGameObj();
        this._timeAccumulate+=this.refreshTime;
        if(this._timeAccumulate>this.difficultyAddTime){
            this.difficultyLevel+=1;
            this._timeAccumulate =0;
        }
    },

    //所有物体移动
    LoopMove(){
        for(let value in this._CollisionList){

            let mm= this._CollisionList[value].getComponent(modelManager);
            if(mm!==null)
            {

                if(mm.isOver){
                    this._CollisionList.splice(value,1);

                }else
                {
                    mm.UpdateFun();
                }
            }

        }
    },
    LoopPropSpawn(){
        //cc.log("道具生成");
        let randomvalue= cc.random0To1()*(this.PropPrefab.length-1);
        //cc.error("随机值是"+randomvalue);
        let pre= cc.instantiate(this.PropPrefab[Math.round(randomvalue)]);
        pre.setParent(this.canvasRoot);

        let wid =this.canvasRoot.width-100;
        let posX =(cc.random0To1()*wid)-(wid/2);

        pre.position=cc.p(posX,this.canvasRoot.height);
        //加入列表
        this._CollisionList.push(pre);
    },


    SpawnGameObj()
    {
        let randomvalue= cc.random0To1()*(this.blocksPrefab.length-1);
        let pre= cc.instantiate(this.blocksPrefab[Math.round(randomvalue)]);
        pre.setParent(this.canvasRoot);
        let wid =this.canvasRoot.width-100;
        let posX =(cc.random0To1()*wid)-(wid/2);

        pre.position=cc.p(posX,this.canvasRoot.height);
        let mm= pre.getComponent(modelManager);
        mm.StartRandomPickValue(this.difficultyLevel);
        this._CollisionList.push(pre);

    },


    GameOver(){
        this._isStart =false;
        this.unschedule(this.LoopSpawn,this);
        this.unschedule(this.LoopMove,this);
        this.unschedule(this.LoopPropSpawn,this);
        this.ClearObj();
        this._layout.destroy();
    },

    ClearObj(){



        for(let objIndex in this._CollisionList){
            this._CollisionList[objIndex].destroy();
        }
        this._CollisionList.length =0;
    },

     update (dt) {

        if(this._CollisionList.length===0){
            return;
        }

         for(let objIndex in this._CollisionList){

             let valueY=this._CollisionList[objIndex].y; //这个为方块的父物体
             if(valueY<=-100){
             //if(valueY+50+(this.canvasRoot.height/2-500)<0){ //这里的200为屏幕下方向上的偏移
                 //cc.log("销毁");

                 this._CollisionList[objIndex].destroy();

                 this._CollisionList.splice(objIndex,1);
             }
         }

     },


    //PropFun

    //爆炸
    Explode(targetpos,length2)
    {
        for(let value of this._CollisionList){
            let mm= value.getComponent(modelManager);
            mm.Explode(targetpos,length2);
        }


    }

});
